package top.eggcode.common.mvc;

import top.eggcode.common.consts.ResponseStatus;
import top.eggcode.common.model.ResponseResult;

import java.io.Serializable;

/**
 * @author huangxunhui
 * Date: Created in 18/8/20 上午11:05
 * Utils: Intellij Idea
 * Description: 返回数据工具类
 */
public final class Responser implements Serializable {

    /**
     * 成功
     *
     * @param object 需要返回的数据
     * @return data
     */
    public static <T> ResponseResult<T> successful(T object) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(ResponseStatus.OK.getCode());
        result.setMessage(ResponseStatus.OK.getMsg());
        result.setData(object);
        return result;
    }

    /**
     * 成功
     *
     * @return 返回空
     */
    public static <T> ResponseResult<T> successful() {
        return successful(null);
    }

    /**
     * 错误
     *
     * @param responseStatus 错误枚举类
     * @return 错误信息
     */
    public static ResponseResult<?> failure(ResponseStatus responseStatus) {
        ResponseResult<?> result = new ResponseResult<>();
        result.setCode(responseStatus.getCode());
        result.setMessage(responseStatus.getMsg());
        return result;
    }

    /**
     * 错误
     *
     * @param code 状态码
     * @param msg  消息
     * @return ResultBean
     */
    public static <T> ResponseResult<T> failure(Integer code, String msg) {
        ResponseResult<T> result = new ResponseResult<T>();
        result.setCode(code);
        result.setMessage(msg);
        return result;
    }

    /**
     * 错误
     *
     * @param msg 错误信息
     * @return ResultBean
     */
    public static <T> ResponseResult<T> failure(String msg) {
        return failure(-1, msg);
    }

}
